package com.example.octopus.controller;

import com.alibaba.fastjson2.JSON;
import com.example.octopus.entity.Collect;
import com.example.octopus.entity.Comment;
import com.example.octopus.entity.returnMessage.*;
import com.example.octopus.service.CollectService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("api/collect/collect")
public class CollectContorller {
    @Autowired
    CollectService collectService;
    @Autowired
    Gson gson;
    private static final String TrueNumber ="0";
    private static final int TureReturnCode =200;
    private static final int ErrReturnCode =500;
    //等待yml注入
    String env="cloud1-6gma1k2s0ff0ac6b";
    @PostMapping
    public SendReturnMessage InsertCollect(@RequestBody Collect collect){
        String randomString = UUID.randomUUID().toString().replaceAll("-", "");
        if (randomString.length() != 32) {
            System.out.println("生成字符串长度不正确，应该为32位。");
        } else {
            // 输出生成的32位随机字符串
            System.out.println(randomString);
        }
        collect.set_id(randomString);
        if(collect==null){
            return new SendReturnMessage(ErrReturnCode,"传参错误");
        }
        Map<String,Object> conditions=new HashMap<>();
        if(collect.getProductId()==null||collect.get_openid()==null|| collect.get_openid().isEmpty() || collect.getProductId().isEmpty()){
            return new SendReturnMessage(ErrReturnCode,"缺少产品id或openid");
        }
        conditions.put("productId",collect.getProductId());
        conditions.put("_openid",collect.get_openid());

        if(collectService.select(env,conditions,0).getData().isEmpty()) {
            ArrayList<Collect> collects = new ArrayList<>();
            collects.add(collect);
            InsertReturnMessage insertReturnMessage = collectService.insert(env, collects);
            if (insertReturnMessage.getErrcode().equals(TrueNumber)) {
                return new SendReturnMessage(TureReturnCode, insertReturnMessage.getErrmsg());
            } else {
                return new SendReturnMessage(ErrReturnCode, insertReturnMessage.getErrmsg());
            }
        }else {
            System.out.println(collectService.select(env,conditions,0).getData());
            return new SendReturnMessage(ErrReturnCode,"已收藏！");
        }
    }
    @PostMapping("getCollect/pages/{page}")
    public CollectSendSelectMessage select(@RequestBody Collect collect,@PathVariable int page){
        Map<String,Object> conditions=new HashMap<>();
        if(collect==null||collect.get_openid()==null||collect.get_openid().isEmpty()){
            return new CollectSendSelectMessage(null,ErrReturnCode,"无效或空openid");
        }
        conditions.put("_openid",collect.get_openid());
        if(collect.getProductId()!=null){
            conditions.put("productId",collect.getProductId());
        }
        SelectReturnMessage selectReturnMessage=collectService.select(env,conditions,page);
        if(selectReturnMessage.getErrcode().equals(TrueNumber)){
            List<Collect> collects=new ArrayList<>();
            for(String s: selectReturnMessage.getData()){
                Collect collect1= JSON.parseObject(s,Collect.class);
                collects.add(collect1);
            }
            return new CollectSendSelectMessage(collects,TureReturnCode,selectReturnMessage.getErrmsg());
        }else{
            return new CollectSendSelectMessage(null,ErrReturnCode,selectReturnMessage.getErrmsg());
        }
    }
    @DeleteMapping
    public SendReturnMessage delete(@RequestBody Collect collect){
        Map<String,Object> condtions=new HashMap<>();
        if(collect==null||collect.get_openid()==null||collect.get_openid().isEmpty()){
            return new SendReturnMessage(ErrReturnCode,"产品参数出错");
        }
        condtions.put("_openid",collect.get_openid());
        if(collect.getProductId()!=null){
            condtions.put("productId",collect.getProductId());
        }else{
            return new SendReturnMessage(ErrReturnCode,"缺少产品id!");
        }
        DeleteReturnMessage deleteReturnMessage=collectService.delete(env,condtions);
        if(deleteReturnMessage.getErrcode().equals(TrueNumber)){
            return new SendReturnMessage(TureReturnCode, deleteReturnMessage.getErrmsg());
        }else {
            return new SendReturnMessage(ErrReturnCode, deleteReturnMessage.getErrmsg());
        }
    }
}

